package com.rmb.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.rmb.dao.TaskInfoDao;
import com.rmb.entity.TaskInfo;
import com.rmb.service.WriteExcelService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @version 1.0
 * @Author y
 * @Date 2021/1/8 19:40
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WriteExcelServiceImpl implements WriteExcelService {
    @Autowired
    TaskInfoDao taskInfoDao;
    @Override
    public void writeexcel(String dizhi, String sname, String xname) throws IOException {
        //获取数据
        List<TaskInfo> all = taskInfoDao.findAll();
        //导出Excel
        //参数1：exportParams导出配置对象，参数2：导出的类型 参数3：导出数据集合
        Workbook wb = ExcelExportUtil.exportExcel(new ExportParams(sname, xname), TaskInfo.class, all);
        //将Excel写入指定位置
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(dizhi);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        wb.write(outputStream);
        outputStream.close();
    }
}
